package com.woniuxy.lab.woniu_lab.util;

import cn.hutool.core.lang.Snowflake;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

public class OSSNewUtil {

    @Value("${aliOSS.EndPoint}")
    public static String endPoint;// 地域节点
    @Value("${aliOSS.BucketURL}")
    public static String bucketURL;// bucket公网域名
    @Value("${aliOSS.BucketName}")
    public static String bucketName;// bucket名称
    @Value("${aliOSS.AccessKeyID}")
    public static String accessKeyID;
    @Value("${aliOSS.AccessKeySecret}")
    public static String accessKeySecret;

    @SneakyThrows
    public static String uploadFile(MultipartFile file){
        String orderNo = new Snowflake(1, 1).nextIdStr();// 雪花算法生成随机字符串
        String rawName = file.getOriginalFilename();// 获取原始文件名称
        String fileName = rawName + orderNo;
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyID, accessKeySecret);// 创建OSS连接服务对象
        ossClient.putObject(bucketName, fileName, file.getInputStream());// 向bucketName这个桶容器存入名为rawName的文件（流形式上传）
        return bucketURL + fileName;// 文件的url
    }

}
